/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.stat.content.log;

import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.RetryRecord;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.stat.StatMessages;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class ViewRetryPanel extends Composite {

	private StatMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private GwtContext context;

	private SimplePanel panel;
	
	private VerticalPanel vp;

	private DialogBox box;

	private Grid table;
	
	private boolean loaded;

	// private HashMap<String,String> gIds;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				box.hide();
				return;
			}

			ArrayList<RetryRecord> rs = (ArrayList<RetryRecord>) res
					.getResponse();
			//panel.clear();

			if (rs != null && rs.size() > 0) {

				table = new Grid(rs.size() + 1, 3);
				table.setWidth("100%");
				table.setCellPadding(1);
				table.setCellSpacing(1);
				table.setBorderWidth(1);

				ColumnFormatter cf = table.getColumnFormatter();

				cf.setWidth(0, "40%");
				cf.setWidth(1, "30%");
				cf.setWidth(2, "30%");

				CellFormatter ff = table.getCellFormatter();
				ff.setStyleName(0, 0, "tablecol");
				ff.setStyleName(0, 1, "tablecol");
				ff.setStyleName(0, 2, "tablecol");

				table.setHTML(0, 0, cons.time());

				table.setHTML(0, 1, cons.status());

				table.setHTML(0, 2, "&nbsp;");

				for (int i = 1; i < rs.size() + 1; i++) {
					RetryRecord rr = rs.get(i - 1);
					table.setHTML(i, 0, rr.getRetryTime());
					String status = null;

					if (rr.getStatus().equals(EntityConstants.STATUS_OK)) {
						status = cons.success();
					} else {
						status = cons.error();
					}
					table.setHTML(i, 1, status);

					Button b = new Button(cons.detail());
					ViewClickListener vl = new ViewClickListener();
					vl.setId(rr.getId());
					setViewListenerPanel(vl);

					b.addClickListener(vl);

					table.setWidget(i, 2, b);

				}

				//panel.add(table);
				// panel.add(new HTML("&nbsp;"));
				vp = new VerticalPanel();
				vp.setWidth("100%");
				vp.setSpacing(5);
				vp.add(table);
//				Button re = new Button(cons.close());
//				re.addClickListener(new ClickListener() {
//
//					public void onClick(Widget sender) {
//						box.hide();
//
//					}
//				});
//				vp.add(re);
				panel.setWidget(vp);

			} else {
				panel.add(new HTML(cons.noRetry()));
			}

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	private void setViewListenerPanel(ViewClickListener vl) {
		vl.setPanel(this);
	}

	public ViewRetryPanel(StatMessages cons, BifangGWTServiceAsync service,
			GwtContext ctx, DialogBox box1) {
		this.service = service;
		this.cons = cons;
		this.box = box1;
		this.context = ctx;
		VerticalPanel vp = new VerticalPanel();
		vp.setWidth("100%");
		vp.setSpacing(5);

		panel = new SimplePanel();
		panel.setWidth("100%");

		Button re = new Button(cons.close());
		re.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				box.hide();

			}
		});

		vp.add(panel);
		vp.add(re);
		initWidget(vp);

	}

	public void onLoad() {
		if(loaded)
			return;
		RequestObject req = new RequestObject();
		req.setAction("han.bifang.gwt.server.action.stat.LogDetailAction");
		req.setRequest("retry");
		service.call(req, cb);
		loaded = true;
	}

	public void setTable() {
		panel.setWidget(vp);
	}

	private static class ViewClickListener implements ClickListener {

		private String id;

		private ViewRetryPanel vrp;

		public ViewRetryPanel getPanel() {
			return vrp;
		}

		public void setPanel(ViewRetryPanel panel) {
			this.vrp = panel;
		}

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public void onClick(Widget sender) {
			RetryDetailPanel rdp = new RetryDetailPanel(vrp.cons, vrp.service,
					vrp.context, vrp, id);
			vrp.panel.setWidget(rdp);

		}

	}

}
